/* 合同中心》合同关联》新增关联 */
<template>
  <div>
    <div id="samplesFixed" :class="{'samples-fixed' : isFixed}">
      <el-row class="samples-info-box insert-page">
        <el-col :span="15">
          <h2>合同关联</h2>
        </el-col>
        <!-- 右半边按钮区域 -->
        <el-col :span="9" class="samples-text-right">
          <el-button size="mini" type="primary" plain @click="CloseBtn">取 消</el-button>
          <el-button size="mini" type="primary" @click="confirmBtn('1')">确 定</el-button>
          <el-button size="mini" type="primary" @click="confirmBtn('2')">关联完成</el-button>
        </el-col>
      </el-row>
    </div>
    <contract-list :give-single-name="giveProjectName" contract-type="收入合同" @pushData="IncomeData" />
    <contract-list :give-project-name="giveSingleName" contract-type="支出合同" @pushData="expendData" />
  </div>
</template>

<script>
import { cacheMixin } from '@/utils/mixins/clearCache'
import { fixedMixin } from '@/utils/mixins/fixedHeaderMixin'
import contractList from './components/contractList'
import * as contractCorrelationApi from '@/api/contractManage/contractCorrelation'
export default {
  components: {
    contractList
  },
  mixins: [cacheMixin, fixedMixin],
  data() {
    return {
      isFixed: false,
      income: [],
      expend: [],
      contractId: '',
      beContractId: [],
      giveSingleName: '',
      giveProjectName: '',
      confirmData: {}
    }
  },
  methods: {
    // 点击取消按钮  跳转到列表页面
    CloseBtn() {
      // this.clearCache('contractCorrelation/insert')
      // this.closeTag('/contractManage/contractCorrelation')
      this.clearCache('/contractManage/contractCorrelation/insert')
      this.closeTag('/contractManage/contractCorrelation')
    },
    // cancelBtn() {
    //   alert(2)
    //   // this.$router.push({ path: '/contractManage/contractCorrelation' })
    //   this.closeTag('/contractManage/contractCorrelation')
    // },
    // 点击确定按钮按钮  提交选中的合同
    confirmBtn(val) {
      if (!this.income.length) {
        this.$notify({
          title: '警告',
          message: '请选择收入合同',
          type: 'warning',
          duration: '3000'
        })
        return false
      }
      if (!this.expend.length) {
        this.$notify({
          title: '警告',
          message: '请选择支出合同',
          type: 'warning',
          duration: '3000'
        })
        return false
      }
      console.log('我点击了确定按钮')
      const obj = {
        'contractId': this.contractId,
        'beContractId': this.beContractId,
        'type': val,
        'id': ''
      }
      console.log('我点击了确定按钮')
      console.log(obj)
      contractCorrelationApi.contractRelationAdd(obj).then(res => {
        console.log('关联成功', res.data)
        if (res.code == 1) {
        //   alert(123)
          this.$notify({
            title: '成功',
            message: '新增成功',
            type: 'success',
            duration: '3000'
          })
          this.CloseBtn()
        } else {
          this.$notify.error({
            title: '错误',
            message: res.msg,
            duration: '3000'
          })
        }
      })
    },
    // 获取要关联的合同
    IncomeData(val) {
      console.log('收入合同传上来的值', val)
      this.income = val
      this.contractId = val[0].id
      if (this.contractId) {
        this.giveSingleName = val[0].classifyId
      } else {
        this.giveSingleName = ''
      }
    },
    expendData(val) {
      console.log('支出合同传上来的值', val)
      this.expend = val
      this.beContractId = []
      for (let i = 0; i < val.length; i++) {
        this.beContractId[i] = val[i].id
      }
      console.log(this.beContractId)
      if (this.beContractId.length) {
        this.giveProjectName = val[0].classifyId
      } else {
        this.giveProjectName = ''
      }
    }
  }
}
</script>

<style>

</style>
